﻿using System.Linq;
using PersonaLibrary.Data;
using System.Reflection;
using System.Collections.Generic;

namespace PersonaLibrary.ViewModel.ExcelViewModel
{
    public class ExportAllVM : BaseExcelVM
    {
        private readonly LinqToDbDataContext contex = new LinqToDbDataContext();
        private IEnumerable<Employee> collection;

        public  ExportAllVM()
            : base()
        {
            collection = contex.Employees;
        }

        public ExportAllVM(IEnumerable<Employee> arg)
            : base()
        {
            collection = arg;
        }

        protected override void CommitOperation()
        {
            const int columns = 22;
            int all = collection.Sum(x => x.ConnectionJobEmplDeps.Count);
            var stringRange = new string[all, columns];
            int count = 0;

            foreach (var employeeEntity in collection)
            {
                foreach (var con in employeeEntity.ConnectionJobEmplDeps)
                {
					if(employeeEntity.UpdateDate.HasValue)
						stringRange[count, 0] = employeeEntity.UpdateDate.Value.ToShortDateString();
                    stringRange[count, 1] = employeeEntity.Surname;
                    stringRange[count, 2] = employeeEntity.Name;
                    stringRange[count, 3] = employeeEntity.Patronymic;
                    if (employeeEntity.Birthday != null)
                        stringRange[count, 4] = employeeEntity.Birthday.Value.ToShortDateString();
                    stringRange[count, 5] = con.Department.Organization.Ownership;
                    stringRange[count, 6] = con.Department.Organization.Name;
                    stringRange[count, 7] = con.Department.Organization.Category;
                    stringRange[count, 8] = con.Department.Name;
                    stringRange[count, 9] = con.Job.Name;
                    stringRange[count, 12] = con.Department.Telephone;
                    stringRange[count, 13] = employeeEntity.PersonalPhone;
                    stringRange[count, 15] = con.Department.Office;
                    stringRange[count, 16] = con.Department.Email;
                    stringRange[count, 17] = con.Department.Address;
                    stringRange[count, 21] = con.Department.Organization.Site;

                    ++count;
                }
            }

            var range = OpenedWorksheet.Range[Offset, Missing.Value];
            range = range.Resize[all, columns];

            range.set_Value(Missing.Value, stringRange);

            base.CommitOperation();
        }
    }
}
